package cn.flkj.lucia2a2.intermediate;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.NullValueInNestedPathException;
import org.springframework.cache.support.NullValue;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 州长在手 2021/1/9 上午10:28
 */
public class FabricManagerMost {

    private final Map<String ,IntermediateOrg> orgs = new HashMap<>();

    public FabricManagerMost(IntermediateOrg... orgs)  {
       for (IntermediateOrg org : orgs){
           this.orgs.put(org.getOrgName(),org);
       }
    }

    /**
     * 安装智能合约
     *
     */
    public JSONObject lifecycleChaincodeInstall(
            String chaincodeName,
            String chaincodeVersion,
            String chaincodeLabel, // 标签，是名字加版本号的结合
            String chaincodeSourceLocation, // GOPATH
            String chaincodePath, // 相对于GOPATH的路径
            String metadadataSource, // metadadataSource,一般是couchdb的连接方式JSON的路径
            String chaincodeEndorsementPolicyPath // 策略文件
    ) throws Exception {
        IntermediateLifecycleChaincode ic = new IntermediateLifecycleChaincode(
                chaincodeName,
                chaincodeVersion,
                chaincodeLabel,
                chaincodeSourceLocation,
                chaincodePath,
                metadadataSource,
                chaincodeEndorsementPolicyPath,
                null);
        String pkid = null;
        try {
            pkid = ic.install(orgs);
        }catch (Exception e){
            System.out.println("Install code >>> "+ e.getMessage());
        }
        return JSONObject.parseObject( "{ "+ "\"package_id\":"+"\""+ pkid+"\"}") ;
    }



    /**
     * 审批链码
     */
    public JSONObject lifecycleChaincodeApproveformyorg( String chaincodeName,
                                                         String chaincodeVersion,
                                                         String packageid) {
        IntermediateLifecycleChaincode ic = new IntermediateLifecycleChaincode(chaincodeName,chaincodeVersion,packageid);
        try {
            ic.approveformyorg(orgs);
        }catch (Exception e){
            System.out.println("Approveformyorg code >>> "+ e.getMessage());
        }
        return new JSONObject();
    }



    public JSONObject lifecycleChaincodeCommit( String chaincodeName,
                                                String chaincodeVersion,
                                                String packageid) {
        IntermediateLifecycleChaincode ic = new IntermediateLifecycleChaincode(chaincodeName,chaincodeVersion,packageid);
        try {
            // 向通道提交链码
            ic.commit(orgs.get("org1"));
        }catch (Exception e){
            System.out.println("commit code >>> "+ e.getMessage());
        }
        return new JSONObject();
    }

    /**
     * lifecycle Chaincode Init
     */
    public JSONObject lifecycleChaincodeInit(String chaincodeName,
                                             String chaincodeVersion,
                                             String packageid) {
        IntermediateLifecycleChaincode ic = new IntermediateLifecycleChaincode(chaincodeName,chaincodeVersion,packageid);
        try {
            // 手动执行chaincode init 函数, ，后期加入初始指定的函数
            ic.initChaincode(orgs.get("org1"));
        }catch (Exception e){
            System.out.println("init code >>> "+ e.getMessage());
        }
        return new JSONObject();
    }



    /**
     * 添加链码，只需要执行链码名称即可
     * @param chaincodeName
     */
    public void addChainCode(String chaincodeName){
        for(IntermediateOrg org : orgs.values()) {
            IntermediateChaincodeID intermediateChaincodeID = new IntermediateChaincodeID();
            intermediateChaincodeID.setChaincodeName(chaincodeName);
            org.addChaincode(intermediateChaincodeID);
        }
    }
}
